package com.work.simpleoa.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;


/**
 * @description: 文件处理工具类
 * @author: liuwangzhen
 * @date: 2018-12-17 上午10:26
 */
public class FileUtils {

    private static final Logger log = LoggerFactory.getLogger(FileUtils.class);


    /**
     *
     * @param path
     * @param propertiesFilename
     * @return File resultFile
     */
    public static File getPropertiesFile(String path,String propertiesFilename) {
        File resultFile = null;
        //判断当前输入的路径是否为项目路径
        if (System.getProperty("user.dir").equals(path)) {
            //读取classpath路径下的文件资源
            Enumeration<URL> urlEnumeration = classPathLoadResource("");
            if (urlEnumeration != null) {
                while (urlEnumeration.hasMoreElements()) {
                    String filename = urlEnumeration.nextElement().getFile();
                    //看看当前文件是否为文件夹
                    if (filename.endsWith(File.separator)) {
                        //读取文件夹中的文件资源
                        resultFile = loadFromDir(filename,propertiesFilename);
                    } else {
                        //如果不是文件夹,以目标文件名为后缀,则说明就是该文件
                        if (filename.endsWith(propertiesFilename)){
                            resultFile = new File(filename);
                        }
                    }
                }
            }
        } else {
            //从绝对路径中读取配置文件
            if (path.endsWith(File.separator)){
                resultFile = loadFromDir(path, propertiesFilename);
            }else{
                resultFile = new File(path);
            }
        }
       return resultFile;
    }

    /**
     * 从文件夹中查找是否存在对应的文件
     * @param dirname
     * @param propertiesFilename
     * @return File resultFile
     */
    private static File loadFromDir(String dirname,String propertiesFilename){
        File resultFile = null;
        //将文件加看作成一个目录,对文件夹信息进行一个过滤
        File file = new File(dirname);
        String[] filenameArray = file.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(propertiesFilename);
            }
        });
        //数组转list
        if (filenameArray != null && filenameArray.length > 0) {
            for (String childfilename : filenameArray) {
                if (childfilename.equals(propertiesFilename)) {
                    resultFile = new File(childfilename);
                }
            }
        }
        return resultFile;
    }

    /**
     * 从类文件夹下加载
     */
    public static Enumeration<URL> classPathLoadResource(String classpath){
        Enumeration<URL> resources = null;
        try {
            resources = FileUtils.class.getClassLoader().getResources(classpath);
           // log.error("类文件夹");
        } catch (IOException ioe) {
            log.error("类文件夹下未找到对应的文件");
        }
        return resources;
    }
    //public static void main(String[] args) {
//        File propertiesFile = getPropertiesFile(System.getProperty("user.dir"), "myBeetl.properties");
//        if (propertiesFile != null) {
//            System.out.println("所查询的对应的文件存在,"+propertiesFile.getPath());
//        } else {
//            System.out.println("所查询的对应的文件不存在,");
//        }
//        System.out.println(System.getProperty("user.dir"));
//        Enumeration<URL> urlEnumeration = classPathLoadResource("");
//        System.out.println("urlEnumeration = [" + urlEnumeration.toString() + "]");
//        while (urlEnumeration.hasMoreElements()) {
//            System.out.println("file:"+urlEnumeration.nextElement().getFile());
//
//        }
//    }

}
